package com.xiguaji.tiktok.task.blogger.event;

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.gugee.tiktok.common.model.dal.blogger.BloggerMainRepository;
import com.xiguaji.boot.eventbus.Event;
import com.xiguaji.boot.eventbus.EventBusTemplate;
import com.xiguaji.boot.eventbus.IEventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

@Component
public class RefreshAuditBloggerMainEventListener implements IEventListener<RefreshAuditBloggerMainEventListener.RefreshAuditBloggerMainEvent> {

    @Autowired
    BloggerMainRepository bloggerMainRepository;

    @Autowired
    @Lazy
    EventBusTemplate eventBusTemplate;

    @Override
    @Subscribe
    @AllowConcurrentEvents
    public void handle(RefreshAuditBloggerMainEventListener.RefreshAuditBloggerMainEvent event) {
        run(event);
    }

    public void run(RefreshAuditBloggerMainEventListener.RefreshAuditBloggerMainEvent event) {
        var bloggers = bloggerMainRepository.findAuditSlimBloggers();

        var saveBloggerEvent = new UpdateBloggerMainEventListener.UpdateBloggerMainEvent(event, bloggers);
        eventBusTemplate.post(saveBloggerEvent);
    }

    public static class RefreshAuditBloggerMainEvent extends Event {

        public RefreshAuditBloggerMainEvent() {
            super("RefreshAuditBloggerMain");
        }
    }
}
